/**
 * 权限申请服务
 * 提供权限申请的相关API接口
 */
import apiClient from './apiClient'

export const PermissionRequestService = {
  /**
   * 提交权限申请
   * @param {Object} requestData - 申请数据
   * @param {string} requestData.permission - 申请的权限标识
   * @param {string} requestData.role - 申请的角色（可选）
   * @param {string} requestData.reason - 申请原因
   * @param {string} requestData.targetResource - 目标资源/页面
   * @returns {Promise}
   */
  async submitRequest(requestData) {
    try {
      const response = await apiClient.post('/api/permission-requests', requestData)
      console.log('✅ 权限申请已提交:', response)
      return response
    } catch (error) {
      console.error('❌ 提交权限申请失败:', error)
      throw error
    }
  },

  /**
   * 获取我的权限申请列表
   * @param {Object} params - 查询参数
   * @returns {Promise}
   */
  async getMyRequests(params = {}) {
    try {
      const response = await apiClient.get('/api/permission-requests/my', { params })
      console.log('✅ 获取我的权限申请成功:', response)
      return response
    } catch (error) {
      console.error('❌ 获取权限申请失败:', error)
      throw error
    }
  },

  /**
   * 获取权限申请详情
   * @param {string|number} id - 申请ID
   * @returns {Promise}
   */
  async getRequestById(id) {
    try {
      const response = await apiClient.get(`/api/permission-requests/${id}`)
      console.log('✅ 获取权限申请详情成功:', response)
      return response
    } catch (error) {
      console.error('❌ 获取权限申请详情失败:', error)
      throw error
    }
  },

  /**
   * 取消权限申请
   * @param {string|number} id - 申请ID
   * @returns {Promise}
   */
  async cancelRequest(id) {
    try {
      const response = await apiClient.delete(`/api/permission-requests/${id}`)
      console.log('✅ 权限申请已取消:', response)
      return response
    } catch (error) {
      console.error('❌ 取消权限申请失败:', error)
      throw error
    }
  },

  /**
   * 获取所有权限申请（管理员）
   * @param {Object} filters - 过滤条件
   * @returns {Promise}
   */
  async listAllRequests(filters = {}) {
    try {
      const response = await apiClient.get('/api/permission-requests', { params: filters })
      console.log('✅ 获取所有权限申请成功:', response)
      return response
    } catch (error) {
      console.error('❌ 获取权限申请列表失败:', error)
      throw error
    }
  },

  /**
   * 批准权限申请（管理员）
   * @param {string|number} id - 申请ID
   * @param {string} comment - 审批意见
   * @returns {Promise}
   */
  async approveRequest(id, comment = '') {
    try {
      const response = await apiClient.post(`/api/permission-requests/${id}/approve`, { comment })
      console.log('✅ 权限申请已批准:', response)
      return response
    } catch (error) {
      console.error('❌ 批准权限申请失败:', error)
      throw error
    }
  },

  /**
   * 拒绝权限申请（管理员）
   * @param {string|number} id - 申请ID
   * @param {string} reason - 拒绝原因
   * @returns {Promise}
   */
  async rejectRequest(id, reason = '') {
    try {
      const response = await apiClient.post(`/api/permission-requests/${id}/reject`, { reason })
      console.log('✅ 权限申请已拒绝:', response)
      return response
    } catch (error) {
      console.error('❌ 拒绝权限申请失败:', error)
      throw error
    }
  }
}

export default PermissionRequestService

